package cn.iocoder.yudao.module.blog.convert.space;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.controller.app.post.vo.AppBlogPostRespVO;
import cn.iocoder.yudao.module.blog.controller.app.space.vo.AppBlogSpaceRespVO;
import cn.iocoder.yudao.module.blog.convert.base.BlogConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.post.BlogPostDO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.blog.controller.admin.space.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.space.BlogSpaceDO;

/**
 * 动态 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface BlogSpaceConvert {

    BlogSpaceConvert INSTANCE = Mappers.getMapper(BlogSpaceConvert.class);

    BlogSpaceDO convert(BlogSpaceCreateReqVO bean);

    BlogSpaceDO convert(BlogSpaceUpdateReqVO bean);

    BlogSpaceRespVO convert(BlogSpaceDO bean);

    List<BlogSpaceRespVO> convertList(List<BlogSpaceDO> list);

    PageResult<BlogSpaceRespVO> convertPage(PageResult<BlogSpaceDO> page);

    List<BlogSpaceExcelVO> convertList02(List<BlogSpaceDO> list);

    default AppBlogSpaceRespVO convertApp(BlogSpaceDO bean, MemberUserRespDTO memberUserRespDTO){
        if ( bean == null ) {
            return null;
        }

        AppBlogSpaceRespVO appBlogSpaceRespVO = new AppBlogSpaceRespVO();

        appBlogSpaceRespVO.setContent(bean.getContent());
        appBlogSpaceRespVO.setMemberId(bean.getMemberId());
        appBlogSpaceRespVO.setStatus(bean.getStatus());
        appBlogSpaceRespVO.setCreateTime(bean.getCreateTime());
        appBlogSpaceRespVO.setId(bean.getId());
        appBlogSpaceRespVO.setPicUrl(bean.getPicUrl());
        appBlogSpaceRespVO.setMember(BlogConvert.INSTANCE.covnertMember(memberUserRespDTO));
        appBlogSpaceRespVO.setLikeNum(bean.getLikeNum());
        appBlogSpaceRespVO.setCommentNum(bean.getCommentNum());

        return appBlogSpaceRespVO;
    }

    default PageResult<AppBlogSpaceRespVO> convertAppPage(PageResult<BlogSpaceDO> page, Map<Long, MemberUserRespDTO> memberMap){
        if ( page == null ) {
            return null;
        }

        PageResult<AppBlogSpaceRespVO> pageResult = new PageResult<AppBlogSpaceRespVO>();
        List<AppBlogSpaceRespVO> list = convertAppList(page.getList(), memberMap);

        pageResult.setList(list);
        pageResult.setTotal(page.getTotal());

        return pageResult;
    }

    default List<AppBlogSpaceRespVO> convertAppList(List<BlogSpaceDO> blogSpaceDOList, Map<Long, MemberUserRespDTO> memberMap){
        List<AppBlogSpaceRespVO> list = new ArrayList<>();
        blogSpaceDOList.forEach(blogSpaceDO -> {
            list.add(convertApp(blogSpaceDO, memberMap.get(blogSpaceDO.getMemberId())));
        });

        return list;
    }

}
